$(function() {
	
	$('img[smile]').bind("click",function(){
		var smile = "：" + $(this).attr("smile") +　":";
		var mainContent = $('#main_content');
		if( mainContent.data("_selection") ){
			mainContent.val(
					mainContent.val().substring(0,mainContent.data("_selection")) + 
					smile + 
					mainContent.val().substring(mainContent.data("_selection")) );
		}else{
			mainContent.val( mainContent.val() + smile );
		}
	});
	
	$('.smileCommon').bind("click",function(){
		var smile = "：" + $(this).attr("smile") +　":";
		var mainContent = $('#main_content');
		if( mainContent.data("_selection") ){
			mainContent.val(
					mainContent.val().substring(0,mainContent.data("_selection")) + 
					smile + 
					mainContent.val().substring(mainContent.data("_selection")) );
		}else{
			mainContent.val( mainContent.val() + smile );
		}
	});
	
	
	$('#main_content').bind("mouseup",function(){
		WSLDS.Utils.textLoc(this);
	});
	
	$('#main_content').bind("keyup",function(){
		WSLDS.Utils.textLoc(this);
	});
	
	$('#choose').bind('click',function(){
		$('#url').val(window.location.pathname);
		
		var answer = $('#answer').val();
		var mainContent = $('#main_content').val();
		var rightAnswer = WSLDS.GLOBLE.ANSWER[$('#answer').data("index")];
		if( answer == rightAnswer ){
			if( $.trim(mainContent).length == 0 ){
				$('#main_content').val("内容不能为空");
				return;
			}else{
				if( "内容不能为空" == mainContent){
					return;
				}
			}
			
			$(this).attr("disabled","disabled");
			$.ajax({
				 type: "POST",
				 url: contextPath + "/review/insert.action" ,
				 data : $('#reviewForm').serializeArray(),
				 success: function(msg){
					if( msg ){
						$('#main_content').val("");
						WSLDS.Method.refreshReviewPage();
						WSLDS.Method.refreshRandomQuestion();
						$('#first_review_anchor').click();
						$('#choose').attr("disabled","");
					}
				 }
			});
			
		}else{
			$('#answer').val("答案不正确呀，给力。");
		}
		
	});
	
	WSLDS.Method.refreshReviewPage();
	$('#reviewClick').click(function(){
		$('#review_anchor').click();
	});
	
	$.ajax({
		 type: "POST",
		 url: contextPath + "/common/reveicePass.action" ,
		 success: function(msg){
			if( msg ){
				var msgArr = msg.split('`');
				WSLDS.GLOBLE.QUESTION = eval(msgArr[0]);
				WSLDS.GLOBLE.ANSWER = eval(msgArr[1]);
				WSLDS.Method.refreshRandomQuestion();
				$('#container_left').height($("#container").height());
				$('#container_right').height($("#container").height());
			}
		 }
	});
	
});


WSLDS.Method.reviewPage = function( path ){
	$.ajax({
		 type: "POST",
		 url: contextPath + "/" + path,
		 success: function(msg){
		 	$('#reviewContent').html(msg);
		 	$('#container_left').height($("#container").height());
			$('#container_right').height($("#container").height());
		 }
	});
};

WSLDS.Method.refreshReviewPage = function(){
	$.ajax({
		 type: "POST",
		 url: contextPath + "/review/listAjax.action?instance.artId=" +  $('#article_id').val() ,
		 success: function(msg){
		 	$('#reviewContent').html(msg);
		 	$('#container_left').height($("#container").height());
			$('#container_right').height($("#container").height());
		 }
	});
};

WSLDS.Method.refreshRandomQuestion = function(){
	var randomNum = Math.round(Math.random()*8);
	$('#question').text(WSLDS.GLOBLE.QUESTION[randomNum]);
	$('#answer').data("index",randomNum);
};

